home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / FXOC.C < prev    next >
Text File  |  1987-06-18  |  4KB  |  146 lines

  1.  
  2. /**
  3. * name         fxu -- function extract utility
  4. *
  5. * usage        fxu filename function
  6. *
  7. *              where "filename" is the name of a file containing
  8. *              several C functions, and "function" is the name of
  9. *              the particular function to be extracted.  If the
  10. *              named function is found, then (1) standard input is
  11. *              copied to the standard output until EOF, and (2) the
  12. *              text of the named function is written to the standard
  13. *              output.  The first option allows header information
  14. *              to be prepended to the output file.
  15. *
  16. **/
  17.  
  18. #include "stdio.h"
  19. #include "ctype.h"
  20.  
  21. #define MAX 16        /* maximum characters in function name */
  22. #define MAXBUF 2000   /* maximum characters buffered between functions */
  23.  
  24. main(argc, argv)
  25. int argc;
  26. char *argv[];
  27. {
  28. int c, brace, cnest, nc;
  29. int i, ns, copy, inlit, delim, pc;
  30. FILE *sfp;
  31. char symbol[MAX+1];
  32. char text[MAXBUF];
  33.  
  34. if (argc != 3)
  35.    {
  36.    fputs("Usage: fxu filename function\n", stderr);
  37.    exit(1);
  38.    }
  39. if ((sfp = fopen(argv[1], "r")) == NULL)
  40.    {
  41.    fputs("Can't open source file\n", stderr);
  42.    exit(1);
  43.    }
  44. brace = cnest = nc = ns = copy = inlit = pc = 0;
  45. c = getc(sfp);        /* get first char */
  46. while (c != EOF)
  47.    {                 /* scan through source file */
  48.    if (ns == MAXBUF)
  49.        {
  50.        fputs("Maximum buffer size exceeded\n", stderr);
  51.        exit(1);
  52.        }
  53.    if (copy == 0)
  54.        {
  55.        if (brace == 0) text[ns++] = c;  /* save chars between functions */
  56.        }
  57.    else
  58.        if (putchar(c) == EOF)
  59.            {
  60.            fputs("Copy error\n", stderr);
  61.            exit(1);
  62.            }
  63.    if (c == '/')
  64.        {             /* possible comment */
  65.        nc = 0;
  66.        if ((c = getc(sfp)) == '*')
  67.            {
  68.            ++cnest;   /* bump nesting level */
  69.            if (copy) putchar(c);
  70.            else if (brace == 0) text[ns++] = c;
  71.            c = getc(sfp);
  72.            }
  73.        continue;
  74.        }
  75.    if (cnest != 0)
  76.        {             /* inside comment */
  77.        if (c == '*')
  78.            {
  79.            if ((c = getc(sfp)) == '/')
  80.               {
  81.               --cnest;       /* reduce nesting level */
  82.               if (copy) putchar(c);
  83.               else if (brace == 0) text[ns++] = c;
  84.               c = getc(sfp);
  85.               }
  86.            continue;
  87.            }
  88.        nc = 0;
  89.        }
  90.    else if (inlit)
  91.        {               /* inside literal string */
  92.        if (c == '\\' && pc == '\\') c = 0;
  93.        if (c == delim && pc != '\\') inlit = 0;
  94.        pc = c;         /* save previous character */
  95.        }
  96.    else if (c == '\'' || c == '\"')
  97.        {               /* enter literal string */
  98.        inlit = 1;
  99.        pc = 0;
  100.        delim = c;
  101.        }
  102.    else if (c == '{') ++brace;
  103.    else if (c == '}')
  104.        {             /* right brace */
  105.        nc = 0;
  106.        if (--brace == 0)
  107.            if (copy == 0) ns = 0;      /* reset save index if not found */
  108.            else
  109.                {               /* copy complete */
  110.                putchar('\n');
  111.                exit(0);
  112.                }
  113.        }
  114.    else if (brace == 0)
  115.        {
  116.        if (nc == 0)
  117.            {             /* symbol not started yet */
  118.            if (iscsymf(c))
  119.                symbol[nc++] = c;  /* start new symbol */
  120.            }
  121.        else if (iscsym(c) || c == '$')
  122.                      /* continue symbol */
  123.            if (nc < MAX) symbol[nc++] = c;
  124.            else symbol[0] = '\0';
  125.        else if (nc != 0)
  126.            {             /* end of current symbol */
  127.            symbol[nc++] = '\0';
  128.            if (strcmp(symbol,argv[2]) == 0)
  129.                {               /* named function has been found */
  130.                while ((c = getchar()) != EOF)
  131.                    putchar(c);         /* copy standard input to output */
  132.                for (i = 0; i < ns; i++)
  133.                    putchar(text[i]);           /* copy saved characters */
  134.                copy = 1;       /* turn on copy flag */
  135.                }
  136.            nc = 0;
  137.            }
  138.        }
  139.    c = getc(sfp);      /* get next char */
  140.    }
  141.  
  142. fputs("Named function not found\n", stderr);
  143. exit(1);
  144. }
  145.  
  146.